#include "app.h"


SemaphoreHandle_t xSensorSemaphore = NULL;  // 信号量句柄
QueueHandle_t xSensorDataQueue = NULL;  // 队列句柄

void sensor_task(void *arg){
    aht21_data_t aht21_data;
    aht21_data_t aht21_data_old;

    while(1)
    {
        // 信号量 + 消息队列 
        if(0 != aht21_read_data(&aht21_data)) {
            log_err("sensor read error!\n");
            continue;
        }
        if((int)aht21_data.temperature != (int)aht21_data_old.temperature || \
        (int)aht21_data.humidity != (int)aht21_data_old.humidity)
        {
            // 2. 发送信号量（通知LVGL线程）
            if (xSemaphoreGive(xSensorSemaphore) != pdTRUE) {
                log_err("Semaphore send error!\n");
            }
            if (xQueueSend(xSensorDataQueue, &aht21_data, pdMS_TO_TICKS(10)) != pdTRUE) {
                log_err("queue full! \n");
            }
            aht21_data_old.temperature = aht21_data.temperature;
            aht21_data_old.humidity = aht21_data.humidity;

            log_info("humidity is %f, temperature is %f\n", aht21_data.humidity,aht21_data.temperature);
        }
        vTaskDelay(500);
    }

}




